using System;
using RomanJendrusz.Amazon.ETL.Exceptions;

namespace RomanJendrusz.Amazon.ETL.Domain
{
    public class Currency
    {
        public static readonly Currency Default = USD;
        //TODO: Add more currencies
        public static readonly Currency USD = new Currency("USD", "US Dollar");

        private readonly string code;
        private readonly string name;

        private Currency(string code, string name)
        {
            if (code == null) throw new ArgumentNullException("code");
            if (name == null) throw new ArgumentNullException("name");
            this.code = code;
            this.name = name;
        }

        public string Code
        {
            get { return code; }
        }

        public string Name
        {
            get { return name; }
        }

        public static Currency GetCurrencyFor(string currencyCode)
        {
            if (currencyCode == null) throw new ArgumentNullException("currencyCode");
            if (currencyCode.Equals("usd", StringComparison.OrdinalIgnoreCase))
            {
                return USD;
            }
            string message = "The currency with code " + currencyCode + " is not supported";
            throw new CurrencyNotSupportedException(message);
        }

        public bool Equals(Currency currency)
        {
            if (currency == null) return false;
            return Equals(code, currency.code) && Equals(name, currency.name);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as Currency);
        }

        public override int GetHashCode()
        {
            return code.GetHashCode() + 29*name.GetHashCode();
        }
    }
}